package io.gitee.zhangbinhub.acp.cloud.server.helloworld.controller;

import io.gitee.zhangbinhub.acp.boot.exceptions.WebException;
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter;
import io.gitee.zhangbinhub.acp.boot.tools.IpTools;
import io.gitee.zhangbinhub.acp.cloud.tools.CloudTools;
import io.gitee.zhangbinhub.acp.cloud.server.helloworld.feign.HelloServer;
import io.gitee.zhangbinhub.acp.cloud.server.helloworld.feign.WorldServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClient;

/**
 * @since JDK 17
 */
@RestController
@RefreshScope
public class HelloWorldController {
    private final LogAdapter logAdapter;
    private final CloudTools cloudTools;
    private final HelloServer helloServer;
    private final WorldServer worldServer;
    private final RestClient.Builder restClientBuilder;
    @Value("${acp.test.properties}")
    private String properties;

    @Autowired
    public HelloWorldController(HelloServer helloServer, WorldServer worldServer, LogAdapter logAdapter, CloudTools cloudTools, @Qualifier(value = "customerRestClientTest") RestClient.Builder restClientBuilder) {
        this.helloServer = helloServer;
        this.worldServer = worldServer;
        this.logAdapter = logAdapter;
        this.cloudTools = cloudTools;
        this.restClientBuilder = restClientBuilder;
    }

    @PostMapping(value = "/helloworld", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<Object> helloWorld(@RequestBody String content) throws WebException {
        String response = helloServer.fromClient(content) + ";" + worldServer.fromClient(content) + "; properties=" + properties;
        logAdapter.info(response);
        return ResponseEntity.ok(response);
    }

    @PostMapping(value = "/helloworld/{type}", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<Object> helloWorld2(@PathVariable("type") String type, @RequestBody String content) throws WebException {
        String response = "[" + type + "] " + helloServer.fromClient(content) + ";" + worldServer.fromClient(content) + "; properties=" + properties;
        logAdapter.info(response);
        return ResponseEntity.ok(response);
    }

    @PostMapping(value = "/exception/1", produces = MediaType.TEXT_PLAIN_VALUE)
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN1')")
    public ResponseEntity<Object> exception1() throws WebException {
        throw new WebException("测试异常");
    }

    @PostMapping(value = "/exception/2", produces = MediaType.TEXT_PLAIN_VALUE)
    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
    public ResponseEntity<Object> exception2() throws WebException {
        throw new WebException("测试异常");
    }

    @GetMapping(value = "/helloworld", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> helloWorldGet(@RequestParam("name") String name) {
        ResponseEntity<String> responseEntity = restClientBuilder.build().get().uri("http://spring-world/world?name={1}", name)
                .accept(MediaType.TEXT_PLAIN)
                .headers(headers -> cloudTools.transmitHeadersServlet(headers, null, true))
                .retrieve()
                .toEntity(String.class);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody());
        return responseEntity;
    }

    @GetMapping(value = "/open/ips", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<Object> ips(HttpServletRequest request) {
        String ip = IpTools.getRemoteIP(request);
        return ResponseEntity.ok(ip);
    }

    @Operation(summary = "获取当前用户token信息", description = "根据当前登录的用户token值，返回详细信息")
    @GetMapping(value = "/token-info", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<BearerTokenAuthentication> currToken(@Parameter(hidden = true) BearerTokenAuthentication bearerTokenAuthentication) {
        return ResponseEntity.ok(bearerTokenAuthentication);
    }

}
